/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package discovery

import (
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/ichain"
	"github.com/pkg/errors"
)

// staticService
// @Description: 所有的inode节点
type staticService struct {
	inodes []ichain.Inode
}

func newStaticService(pvr ichain.IChainProvider, netInodes []ichain.NetworkInode) (ichain.DiscoveryService, error) {
	var inodes []ichain.Inode
	for _, p := range netInodes {
		newInode, err := pvr.CreateInodeFromConfig(&p)
		if err != nil || newInode == nil {
			return nil, errors.WithMessage(err, "NewInode failed")
		}
		inodes = append(inodes, newInode)
	}

	return &staticService{
		inodes: inodes,
	}, nil
}

func NewStaticDiscovery(config ichain.EndpointConfig, pvr ichain.IChainProvider, chainID string) (ichain.DiscoveryService, error) {
	if chainID == "" {
		return nil, errors.New("chain id is nil")
	}
	chainInodes := config.ChainInodes(chainID)
	var netInodes []ichain.NetworkInode
	for _, chainInode := range chainInodes {
		netInodes = append(netInodes, chainInode.NetworkInode)
	}
	return newStaticService(pvr, netInodes)
}

func (ds *staticService) GetInodes() ([]ichain.Inode, error) {
	return ds.inodes, nil
}
